<template>
  <div style="background-color: azure;width: 100%;height: 100%">
    <div class="login-box" @keydown="keydownHandler">
      <div style="float: left">
        <div style="margin-left: 30px;margin-top: 20px;color: white;font-size: 28px">{{welcomeSign}}</div>
        <div style="margin-left: 30px;margin-top: 10px;color: white;font-size: 12px">
          {{systemName}}&nbsp;&nbsp;-&nbsp;&nbsp;{{version}}
        </div>
      </div>
      <div class="login-content">
        <div class="title">
          <span style="margin-left: 10px;">用户登录</span>
        </div>
        <div class="form">
          <div class="form-group">
            <label>账号:</label>
            <input type="text" name="username" autofocus v-model="username" onkeyup="value=value.replace(/[^a-z0-9_]/g,'');" placeholder="请输入账号" id="username">
          </div>
          <div class="form-group">
            <label>密码:</label>
            <input type="password" name="password" v-model="password"  placeholder="请输入密码" id="password">
          </div>
          <div class="form-group">
            <label>验证码:</label>
            <input type="text" name="captcha" autocomplete="off" v-model="captcha" placeholder="请输入验证码"  id="captcha">
          </div>
          <div style="width: 167px;height: 38px;margin-left: 112px;margin-top: 8px" @click="loadCaptcha">
            <i v-if='captchaImage === ""' class="el-icon-loading"/>
            <img v-else :src="captchaImage" width="100%" height="100%" />
          </div>
          <div class="button-box">
            <button type="submit" id="login" @click="doLogin">登&nbsp;&nbsp;&nbsp;&nbsp;录</button>
          </div>
        </div>
        <!--      <div class="bottom">
                Powered by YingCode
              </div>-->
      </div>
    </div>
  </div>
</template>
<style type="text/css" scoped>

* {
  padding: 0;
  margin: 0;
}

input::-webkit-input-placeholder { /* WebKit browsers */
  color:    #d5d5d5;
}
input:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
  color:    #d5d5d5;
}
input::-moz-placeholder { /* Mozilla Firefox 19+ */
  color:    #d5d5d5;
}
input:-ms-input-placeholder { /* Internet Explorer 10+ */
  color: #d5d5d5;
}

.login-box {
  position: absolute;
  width: 750px;
  height: 386px;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  box-shadow: 0px 0px 30px #a9aaab;
  border-radius: 20px;
  background-color: #ffffff;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100% 100%;
  background-image: url('../../assets/login-background.png');
  /*transform: translateX();*/
}

.login-box > .login-content {
  position: relative;
  height: 100%;
  width: 310px;
  float: right;
  margin-right: 20px;
}

.login-box > .login-content > .title {
  height: 70px;
  text-align: center;
  line-height: 70px;
  font-size: 25px;
  padding: 10px;
  color: #1a1f2a;
}

.login-box > .login-content > .form {
  height: 300px;
  line-height: 40px;
  text-align: center;
  margin-top: 10px;
}

.login-box > .login-content > .form .form-group {
  margin-top: 10px;
}

.login-box > .login-content > .form .form-group > label {
  color: #777777;
  font-size: 14px;
  letter-spacing: 5px;
  display: inline-block;
  width: 80px;
  text-align: right;
}

.login-box > .login-content > .form .form-group > input {
  font-size: 14px;
  height: 14px;
  background: #fff0 none;
  outline: none;
  border: 1px solid #ccc;
  width: 50%;
  padding: 5px;
  border-radius: 5px;
}

.login-box > .login-content > .form .form-group > input:focus {
  border: 1px solid #cadef9;
  box-shadow: 0px 0px 8px #77b0ff99;
}

.login-box > .login-content > .form .button-box {
  margin-top: 15px;
}

.login-box > .login-content > .form .button-box button {
  width: 160px;
  height: 30px;
  background-color: #2b3d52;
  border-radius: 20px;
  font-size: 14px;
  margin-left: 29px;
  border: 1px solid transparent;
  outline: none;
  color: #fff;
}

button:hover {
  cursor: pointer;
  color: #ffffffeb !important;
  background-color: #494f5f !important;
}

button:active {
  border: 1px solid #71adff;
}

.login-box > .login-content > .bottom {
  text-align: center;
  color: #8a8a8a;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding-bottom: 29px;
  font-size: 5px;
  margin-left: 18px;
}

</style>
<script>
import {getCaptcha} from "@/api/login/login";
import {getVersion} from "@/utils/system";
export default {
  data() {
    return {
      username: "",
      password: "",
      captcha: "",
      systemName:"YingCode 后台管理系统",
      welcomeSign:"Welcome!",
      captchaImage:""
    }
  },
  beforeMount() {
    this.loadCaptcha()
  },
  computed:{
    version(){
      return getVersion();
    }
  },
  methods: {
    loadCaptcha(){
        getCaptcha().then((rsp) => {
           this.captchaImage = rsp.data;
        });
    },
    keydownHandler(event) {
      if (event.keyCode === 13) {
        this.doLogin();
      }
    },
    doLogin() {
      if (this.username === undefined || this.username.trim() === "") {
        this.$message.error("请输入帐号")
        return;
      }
      if (this.password === undefined || this.password.trim() === "") {
        this.$message.error("请输入密码")
        return;
      }
      if (this.captcha === undefined || this.captcha.trim() === "") {
        this.$message.error("请输入验证码")
        return;
      }
      let loading = this.$loading({fullscreen: true});
      this.$store.dispatch("Login", {
        username: this.username,
        password: this.password,
        captcha: this.captcha
      }).then(() => {
        loading.close();
        if(this.$route.query.redirect){
          this.$router.push({path:this.$route.query.redirect},()=>{});
        }else{
          this.$router.push({path:"/"},()=>{});
        }
      }).catch(reason => {
        loading.close();
        try {
          let error = reason.response.data.error_description;
          if (error === undefined || error.trim() === "") {
            this.$message.error(reason.message);
          } else {
            this.$message.error(error);
          }
        } catch (e) {
          this.$message.error(reason.message);
        }
        this.loadCaptcha();
      })
    }
  }
}
</script>